package com.lanou3g.myapplication.activity.singleton.activity;


import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.PersistableBundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.DrawerLayout;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.widget.Toast;

import com.lanou3g.myapplication.DayNightMode;
import com.lanou3g.myapplication.R;
import com.lanou3g.myapplication.activity.WeatherActivity;
import com.lanou3g.myapplication.adapter.MainAdapter;
import com.lanou3g.myapplication.fragment.FragmentBeauty;
import com.lanou3g.myapplication.fragment.FragmentBoudoir;
import com.lanou3g.myapplication.fragment.FragmentEntertainment;
import com.lanou3g.myapplication.fragment.FragmentFinance;
import com.lanou3g.myapplication.fragment.FragmentFunny;
import com.lanou3g.myapplication.fragment.FragmentHealthy;
import com.lanou3g.myapplication.fragment.FragmentHotspot;
import com.lanou3g.myapplication.fragment.FragmentMilitary;
import com.lanou3g.myapplication.fragment.FragmentRecommended;
import com.lanou3g.myapplication.fragment.FragmentScience;
import com.lanou3g.myapplication.fragment.FragmentSociology;
import com.lanou3g.myapplication.fragment.FragmentSports;
import com.lanou3g.myapplication.fragment.FragmentVideo;

import java.util.ArrayList;
import java.util.Timer;
import java.util.TimerTask;

/**
 * Created by dllo on 16/1/7.
 */
public class MainActivity extends BaseActivity implements View.OnClickListener {
    private ViewPager viewPager;
    private TabLayout tabLayout;
    private ArrayList<Fragment> datas;
    private MainAdapter adapter;
    private DrawerLayout drawerLayout;
    static MainActivity mainActivity;

    @Override
    protected int setLayout() {
        DayNightMode.onActivityCreateSetTheme(this);
        mainActivity=this;
        return R.layout.activity_main;
    }

    @Override
    protected void initView() {
        findViewById(R.id.main_top_left_menu).setOnClickListener(this);
        findViewById(R.id.main_top_right_menu).setOnClickListener(this);
        findViewById(R.id.main_setting).setOnClickListener(this);
        findViewById(R.id.main_landing).setOnClickListener(this);
        viewPager = (ViewPager) findViewById(R.id.main_viewpager);
        viewPager.setOffscreenPageLimit(13);
        tabLayout = (TabLayout) findViewById(R.id.main_tablayout);
        drawerLayout = (DrawerLayout) findViewById(R.id.main_drawer_layout);
        datas = new ArrayList<Fragment>();
        datas.add(new FragmentRecommended());//推荐
        datas.add(new FragmentHotspot());//热点
        datas.add(new FragmentEntertainment());//娱乐
        datas.add(new FragmentSociology());//社会
        datas.add(new FragmentBeauty());//美女
        datas.add(new FragmentFinance());//财经
        datas.add(new FragmentVideo());//视频
        datas.add(new FragmentSports());//体育
        datas.add(new FragmentFunny());//逗比
        datas.add(new FragmentMilitary());//军事
        datas.add(new FragmentBoudoir());//闺房
        datas.add(new FragmentScience());//科技
        datas.add(new FragmentHealthy());//健康
        // Data();
        // recommendedAdapter.addData("564648",1);
        adapter = new MainAdapter(getSupportFragmentManager(), datas, this);
        viewPager.setAdapter(adapter);
        tabLayout.setupWithViewPager(viewPager);
        tabLayout.setTabTextColors(Color.BLACK, Color.RED);
    }

    @Override
    protected void initData() {

    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            exitBy2Click();
        }
        return false;
    }

    private boolean isExit = false;

    private void exitBy2Click() {
        Timer tExit = null;
        if (isExit == false) {
            isExit = true;
            Toast.makeText(this, "双击退出程序", Toast.LENGTH_SHORT).show();
            tExit = new Timer();
            tExit.schedule(new TimerTask() {
                @Override
                public void run() {
                    isExit = false;
                }
            }, 2000);
        } else {
            finish();
            System.exit(0);
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.main_top_left_menu:
                drawerLayout.openDrawer(Gravity.LEFT);

                break;
            case R.id.main_top_right_menu:
                Intent intent3=new Intent(this, WeatherActivity.class);
                startActivity(intent3);
                break;
            case R.id.main_setting:
                Intent intent1 = new Intent(MainActivity.this, SettingActivity.class);
                startActivity(intent1);
                break;
            case R.id.main_landing:
               Intent intent2=new Intent(MainActivity.this,LandingActivity.class);
                startActivity(intent2);
                break;
        }
    }


}
